package org.myself.meeting.service;

import org.myself.meeting.Utils.Constant;
import org.myself.meeting.mapper.DepartmentMapper;
import org.myself.meeting.mapper.EmployeeMapper;
import org.myself.meeting.mapper.NewMessageMapper;
import org.myself.meeting.model.Department;
import org.myself.meeting.model.Employee;
import org.myself.meeting.model.NewMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DepartmentService {
    @Autowired
    DepartmentMapper dm;

    @Autowired
    NewMessageMapper nmmp;

    @Autowired
    EmployeeMapper employeeMapper;

    public Department getDepById(Integer id){
        //调用departmentmapper的接口实现
        return dm.getDepById(id);
    }

    public List<Department> getAllDeps() {
        return dm.getAllDeps();
    }

    public Integer addDepartment(String departmentname, Employee emp) {
        Department department = dm.getDepByName(departmentname);
        if(department != null){
            return -1;
        }
        String operation = emp.getEmployeename()+" 添加部门 "+departmentname;
        nmmp.addNewMessage(Constant.ADDDEPARTMENT,operation,new Date());
        return dm.addDepartment(departmentname);
    }
    //删除部门
    public Integer deleteDepartment(Integer departmentid, String departmentname) {
//        String operation = emp.getEmployeename()+" 删除部门 "+departmentname;
//        nmmp.addNewMessage(Constant.ADDDEPARTMENT,operation,new Date());
        List<Employee> list = employeeMapper.selectAllemp(departmentid);
        for (Employee em :list) {
            employeeMapper.updateDepartment(em.getEmployeeid());
        }
        employeeMapper.deletByDepid(departmentid);

        return dm.deleteDepartment(departmentid);
    }
    //更新部门
    public Integer updateDepartment(Integer id, String name, Employee emp) {
        String operation = emp.getEmployeename()+" 更新部门 "+dm.getDepById(id).getDepartmentname() +" 为 "+name;
        nmmp.addNewMessage(Constant.ADDDEPARTMENT,operation,new Date());
        return dm.updateDepartment(id,name);
    }

}
